/*
 * DxfTables.java
 *
 * Created on 6 de Dezembro de 2007, 11:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package importadorDXF;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author rafaelbuck
 */
public class DxfTables extends DxfSection
    implements DxfTableCollector
{

    public void read(DxfGroups grp, DxfReporter reporter)
        throws DxfException
    {
        do
        {
            int tableLen;
label0:
            {
                grp.read();
                String code = grp.valToString();
                //int tableLen;
                if(grp.number != 0 || !"TABLE".equals(code))
                    if(!"ENDSEC".equals(code))
                        throw new DxfException("err!GrpInTables", new String[] {
                            String.valueOf(grp.getLineNr())
                        });
                    else
                        return;
                grp.read();
                code = grp.valToString();
                if(grp.number != 2)
                    throw new DxfException("err!GrpInTables", new String[] {
                        String.valueOf(grp.getLineNr())
                    });
                if("LAYER".equals(code))
                    activeTable = 0;
                else
                if("LTYPE".equals(code))
                    activeTable = 1;
                else
                if("STYLE".equals(code))
                    activeTable = 2;
                else
                    activeTable = -1;
                tableLen = -1;
                do
                {
                    grp.read();
                    if(grp.number != 70)
                        continue;
                    tableLen = grp.valToInt();
                    if(tableLen <= 0)
                        break label0;
                } while(grp.number != 0);
                grp.unread();
            }
            if(activeTable != -1)
            {
                if(tableLen > 1)
                    tableLen = (int)(1.5D * (double)tableLen);
                else
                    tableLen = 5;
                table[activeTable] = new Hashtable(tableLen);
            }
            grp.readTables(this);
        } while(true);
    }

    void createLayer0(DxfFile file)
    {
        if(table[0] == null)
            table[0] = new Hashtable(1);
        DxfLAYER layer0 = new DxfLAYER("0");
        layer0.finishRead(file);
        activeTable = 0;
        try
        {
            addTable(layer0);
        }
        catch(DxfException _ex) { }
    }

    void createLtypeContinuous(DxfFile file)
    {
        if(table[1] == null)
            table[1] = new Hashtable(1);
        DxfLTYPE conti = new DxfLTYPE();
        conti.finishRead(file);
        activeTable = 1;
        try
        {
            addTable(conti);
        }
        catch(DxfException _ex) { }
    }

    void createStyleStd(DxfFile file)
    {
        if(table[2] == null)
            table[2] = new Hashtable(1);
        DxfSTYLE std = new DxfSTYLE();
        std.finishRead(file);
        activeTable = 2;
        try
        {
            addTable(std);
        }
        catch(DxfException _ex) { }
    }

    public boolean addTable(DxfTable tab)
        throws DxfException
    {
        if(activeTable == -1)
        {
            throw new DxfException("err!Internal");
        } else
        {
            table[activeTable].put(tab.name, tab);
            return true;
        }
    }

    public DxfLAYER getLayer(String name, boolean refer)
    {
        if(table[0] == null)
            table[0] = new Hashtable(1);
        if(name != null)
        {
            name = name.toUpperCase();
            DxfLAYER l = (DxfLAYER)table[0].get(name);
            if(l == null && refer)
            {
                l = new DxfLAYER(name);
                table[0].put(name, l);
            }
            if(refer && l != null)
                l.setReferenced();
            return l;
        } else
        {
            return null;
        }
    }

    public DxfLAYER getLayer(String name)
    {
        return getLayer(name, false);
    }

    public DxfLTYPE getLtype(String name)
    {
        if(name == null)
            name = "CONTINUOUS";
        else
            name = name.toUpperCase();
        if(table[1] != null)
        {
            DxfLTYPE s = (DxfLTYPE)table[1].get(name);
            if(s != null)
            {
                s.setReferenced();
                return s;
            }
            if(name.equals("CONTINUOUS"))
                return null;
            else
                return getLtype("CONTINUOUS");
        } else
        {
            return null;
        }
    }

    public DxfLTYPE[] getLtypes()
    {
        if(table[1] != null)
        {
            DxfLTYPE ret[] = new DxfLTYPE[table[1].size()];
            int l = 0;
            for(Enumeration e = table[1].elements(); e.hasMoreElements();)
                ret[l++] = (DxfLTYPE)e.nextElement();

            return ret;
        } else
        {
            return null;
        }
    }

    public DxfSTYLE getStyle(String name)
    {
        if(name == null)
            name = "STANDARD";
        else
            name = name.toUpperCase();
        if(table[2] != null)
        {
            DxfSTYLE s = (DxfSTYLE)table[2].get(name);
            if(s != null)
            {
                s.setReferenced();
                return s;
            }
            if(name.equals("STANDARD"))
                return null;
            else
                return getStyle("STANDARD");
        } else
        {
            return null;
        }
    }

    Hashtable getLayerTable()
    {
        return table[0] == null ? null : table[0];
    }

    int getLayerTableLength()
    {
        return table[0] == null ? 0 : table[0].size();
    }

    /** Creates a new instance of DxfTables */
    public DxfTables()
    {
        activeTable = -1;
        table = new Hashtable[3];
    }

    static final byte NOT_ACTIVE = -1;
    static final byte LAYER = 0;
    static final byte LTYPE = 1;
    static final byte STYLE = 2;
    static final byte MAX_TABLES = 3;
    protected byte activeTable;
    protected Hashtable table[];
}